<template>
  <div class="app-container">
    <el-form label-width="auto" :model="queryParams" ref="queryForm" :inline="true"
             v-show="showSearch">
      <el-form-item label="荣誉类型名称" prop="gloryTypeName">
        <el-input
          v-model="queryParams.gloryTypeName"
          placeholder="请输入荣誉类型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:type:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:type:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:type:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:type:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-loading"
          size="mini"
          @click="handleFlushCache"
          v-hasPermi="['system:type:list']"
        >刷新缓存
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="荣誉类型" align="center" prop="gloryTypeId"/>
      <el-table-column label="荣誉类型名称" align="center" prop="gloryTypeName" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:type:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:type:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改荣誉类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form label-width="auto" ref="form" :model="form" :rules="rules">
        <el-form-item label="荣誉类型名称" prop="gloryTypeName">
          <el-input v-model="form.gloryTypeName" placeholder="请输入荣誉类型名称"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-divider content-position="center">荣誉数据信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddGloryData">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteGloryData">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="gloryDataList" :row-class-name="rowGloryDataIndex"
                  @selection-change="handleGloryDataSelectionChange" ref="gloryData">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="荣誉排序" prop="glorySort">
            <template slot-scope="scope">
              <el-input v-model="scope.row.glorySort" placeholder="请输入荣誉排序"/>
            </template>
          </el-table-column>
          <el-table-column label="荣誉标签" prop="gloryLabel">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gloryLabel" placeholder="请输入荣誉标签"/>
            </template>
          </el-table-column>
          <el-table-column label="荣誉键值" prop="gloryValue">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gloryValue" placeholder="请输入荣誉键值"/>
            </template>
          </el-table-column>
          <el-table-column label="公章所在单位" prop="gloryFrom">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gloryFrom" placeholder="请输入公章所在单位"/>
            </template>
          </el-table-column>
          <el-table-column label="荣誉分数" prop="gloryGrade">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gloryGrade" placeholder="请输入荣誉分数"/>
            </template>
          </el-table-column>
          <el-table-column label="样式属性" prop="cssClass">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cssClass" placeholder="请输入样式属性"/>
            </template>
          </el-table-column>
          <el-table-column label="表格回显样式" prop="listClass">
            <template slot-scope="scope">
              <el-input v-model="scope.row.listClass" placeholder="请输入表格回显样式"/>
            </template>
          </el-table-column>
          <el-table-column label="是否默认" prop="isDefault">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isDefault" placeholder="请输入是否默认"/>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template slot-scope="scope">
              <el-input v-model="scope.row.status" placeholder="请输入状态"/>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  exportType,
  changeGloryStatus,
  flushCache
} from "@/api/system/type";

export default {
  name: "Type",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedGloryData: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 荣誉类型表格数据
      typeList: [],
      // 荣誉数据表格数据
      gloryDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gloryTypeName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询荣誉类型列表 */
    getList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gloryTypeId: null,
        gloryTypeName: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.gloryDataList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gloryTypeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加荣誉类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const gloryTypeId = row.gloryTypeId || this.ids
      getType(gloryTypeId).then(response => {
        this.form = response.data;
        this.gloryDataList = response.data.gloryDataList;
        this.open = true;
        this.title = "修改荣誉类型";
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.gloryDataList = this.gloryDataList;
          if (this.form.gloryTypeId != null) {
            updateType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gloryTypeIds = row.gloryTypeId || this.ids;
      this.$confirm('是否确认删除荣誉类型编号为"' + gloryTypeIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delType(gloryTypeIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 荣誉数据序号 */
    rowGloryDataIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 荣誉数据添加按钮操作 */
    handleAddGloryData() {
      let obj = {};
      obj.glorySort = "";
      obj.gloryLabel = "";
      obj.gloryValue = "";
      obj.gloryFrom = "";
      obj.gloryGrade = "";
      obj.cssClass = "";
      obj.listClass = "";
      obj.isDefault = "";
      obj.status = "";
      obj.remark = "";
      this.gloryDataList.push(obj);
    },
    /** 荣誉数据删除按钮操作 */
    handleDeleteGloryData() {
      if (this.checkedGloryData.length == 0) {
        this.$alert("请先选择要删除的荣誉数据数据", "提示", {confirmButtonText: "确定",});
      } else {
        this.gloryDataList.splice(this.checkedGloryData[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleGloryDataSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.gloryData.clearSelection();
        this.$refs.gloryData.toggleRowSelection(selection.pop());
      } else {
        this.checkedGloryData = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有荣誉类型数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportType(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    /**  方恒添加的，让方便进行停用用的  */
    handleStatusChange(row) {
      let text = row.status === '0' ? '启用' : '停用'
      this.$confirm('确认要"' + text + '""' + row.gloryTypeId + '"用户吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return changeGloryStatus(row.gloryTypeId, row.status)
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function () {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    /**  方恒添加的，刷缓存  */
    handleFlushCache() {
      this.$confirm('确认要刷缓存吗?【刷新缓存后，用户可以看到最新荣誉类型的改变】', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(function () {
        return flushCache()
      }).then((res) => {
        this.msgSuccess(res.msg)
      })
    }
  }
};
</script>
